package com.axter.libs.photo.bean;

import java.util.ArrayList;
import java.util.List;

/**
 * 文件夹
 * Created by axter on 2015/4/7.
 */
public class MediaFolder {
    /** 文件夹名 */
    private String title;
    /** 文件夹预览图 */
    private String folderThumb;
    /** 文件夹下的资源 */
    private List<MediaInfo> images;

    public MediaFolder(String title, List<MediaInfo> medias) {
        this.title = title;
        if (medias == null) {
            this.images = new ArrayList<>();
        } else {
            this.images = medias;
        }
        this.folderThumb = getFolderThumbPath(medias);
    }

    public MediaFolder(String title, MediaInfo media) {
        if (images == null) {
            images = new ArrayList<>();
        }
        add(media);
        this.title = title;
        this.folderThumb = getFolderThumbPath(media);
    }

    public String getTitle() {
        return title;
    }

    public void setTitle(String title) {
        this.title = title;
    }

    public String getFolderThumb() {
        return folderThumb;
    }

    public void setFolderThumb(String folderThumb) {
        this.folderThumb = folderThumb;
    }

    public List<MediaInfo> getImages() {
        return images;
    }

    public void setImages(List<MediaInfo> images) {
        this.images = images;
    }

    public void add(MediaInfo image) {
        if (image != null) {
            images.add(image);
        }
    }

    public MediaInfo get(int position) {
        return images.get(position);
    }

    public int size() {
        return images.size();
    }

    /**
     * 获取文件夹缩略图
     *
     * @param media
     * @return
     */
    private static String getFolderThumbPath(MediaInfo media) {
        switch (media.getType()) {
            case MediaInfo.TYPE_VIDEO:
                return media.getThumb();
            default:
                return media.getPath();
        }
    }

    /**
     * 获取文件夹缩略图
     *
     * @param medias
     * @return
     */
    private static String getFolderThumbPath(List<MediaInfo> medias) {
        try {
            MediaInfo mediaInfo = medias.get(0);
            if (mediaInfo.getType() == MediaInfo.TYPE_CAMERA) {
                mediaInfo = medias.get(1);
            }
            return getFolderThumbPath(mediaInfo);
        } catch (Exception ex) {
            ex.printStackTrace();
        }
        return "";
    }
}
